﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XDesigner.ORM.DOM;

namespace XDesigner.ORM
{
    [System.Drawing.ToolboxBitmap( typeof( ORMDebuggerControl ))]
    public partial class ORMDebuggerControl : UserControl
    {
        public ORMDebuggerControl()
        {
            InitializeComponent();
        }

        private ORMEngine _ORMEngine = null;

        public ORMEngine ORMEngine
        {
            get { return _ORMEngine; }
            set { _ORMEngine = value; }
        }
         

        public void RefreshView()
        {
            tvwORM.BeginUpdate();
            tvwORM.Nodes.Clear();
            if (this.ORMEngine != null)
            {
                TreeNode projectNode = new TreeNode( this.ORMEngine.Project.Name );
                projectNode.ImageIndex = 0 ;
                projectNode.SelectedImageIndex = 0 ;
                projectNode.Tag = this.ORMEngine.Project ;
                tvwORM.Nodes.Add( projectNode );
                foreach (ORMTypeInfo type in this.ORMEngine.Project.Types)
                {
                    TreeNode typeNode = new TreeNode(type.InstanceType.Name );
                    typeNode.Tag = type;
                    typeNode.ImageIndex = 1;
                    typeNode.SelectedImageIndex = 1;
                    projectNode.Nodes.Add(typeNode);
                    foreach (ORMPropertyInfo p in type.Properties)
                    {
                        TreeNode propertyNode = new TreeNode(p.Name);
                        propertyNode.Tag = p;
                        propertyNode.ImageIndex = 2;
                        propertyNode.SelectedImageIndex = 2;
                        typeNode.Nodes.Add(propertyNode);
                    }
                }//foreach
                projectNode.Expand();
             }
            tvwORM.EndUpdate();
        }

        private void tvwORM_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if ( tvwORM.SelectedNode != null )
            {
                myPropertyGrid.SelectedObject = tvwORM.SelectedNode.Tag;
            }
            else
            {
                myPropertyGrid.SelectedObject = null;
            }
        }

        private void btnSaveProject_Click(object sender, EventArgs e)
        {
            if (this.ORMEngine == null || this.ORMEngine.Project == null)
            {
                MessageBox.Show("Please set ORMEngine.");
                return;
            }
            using( SaveFileDialog dlg = new SaveFileDialog())
            {
                dlg.Filter = "XML file|*.xml";
                dlg.CheckPathExists = true;
                dlg.OverwritePrompt = true;
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    using (System.IO.FileStream stream = new System.IO.FileStream(dlg.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
                    {
                        System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(ORMProjectInfo));
                        ser.Serialize(stream, this.ORMEngine.Project);
                    }
                    MessageBox.Show("Save to " + dlg.FileName);
                }
            }//using
        }

        private void btnGenerateHelperCode_Click(object sender, EventArgs e)
        {
            ORMTypeInfo type = null;
            if (tvwORM.SelectedNode != null)
            {
                type = tvwORM.SelectedNode.Tag as ORMTypeInfo;
            }
            if (type == null)
            {
                MessageBox.Show("Please select a type node");
                return;
            }
            ORMContext context = new ORMContext( this.ORMEngine );
            txtSourceCode.Text = ORMCodeGenerator.GenerateORMHelperClassCode(context, type, null);
        }

        private void btnRefreshPerformance_Click(object sender, EventArgs e)
        {
            myDataGridView.DataSource = ORMPerformanceCounter.GetInstancesStatus();
        }

        private void btnGenerateEntryCode_Click(object sender, EventArgs e)
        {
            ORMTypeInfo type = null;
            if (tvwORM.SelectedNode != null)
            {
                type = tvwORM.SelectedNode.Tag as ORMTypeInfo;
            }
            if (type == null)
            {
                MessageBox.Show("Please select a type node");
                return;
            }
            ORMContext context = new ORMContext(this.ORMEngine);
            txtSourceCode.Text = ORMCodeGenerator.GenerateEntryClassCode( this.ORMEngine.Project , type );
        }
    }
}